function x = CG(A,b,eps)
%UNTITLED 共轭梯度法求解对称正定方程组
%   此处显示详细说明
N = size(A);
n = N(1);
x = linspace(0,1,n)';
r0 = b-A*x;
r = r0;
p = r0;
p0 = p;
cnt = 0;
while vecnorm(r) > eps
    cnt = cnt+1;
    if cnt == 1
        p = r;
        r = r0;
    else
        beta0 = (r'*r)/(r0'*r0);
        p0 = p;
        p = r + beta0*p0;
    end
    alpha = (r'*r)/(p'*A*p);
    x = x + alpha*p;
    r0 = r;
    r = r - alpha*A*p;
end
end

